from rest_framework import serializers

from goods.models import Goods, GoodsCategory, GoodsAlbum


class ParentCategorySerlializer(serializers.ModelSerializer):
    '''商品一级分类序列化器类'''

    class Meta:
        model = GoodsCategory
        fields = ('id', 'title')


class CategorySerlializer(serializers.ModelSerializer):
    '''商品二级分类序列化器类'''
    parent = ParentCategorySerlializer()

    class Meta:
        model = GoodsCategory
        fields = ('id', 'title', 'parent')
        # fields='__all__'


class AlbumSeralizer(serializers.ModelSerializer):
    '''商品图片序列化器类'''

    class Meta:
        model = GoodsAlbum
        fields = ('id', 'original_path', 'thumb_path')


class RecommendedgoodsSerializer(serializers.ModelSerializer):
    '''商品序列化器'''
    category = CategorySerlializer(label='分类')
    goodsalbum_set = AlbumSeralizer(many=True)

    class Meta:
        model = Goods
        fields = "__all__"


class TwoParentCategorySerlializer(serializers.ModelSerializer):
    '''商品分类序列化器类'''
    goodscategory_set = CategorySerlializer(many=True)
    goods = serializers.SerializerMethodField()

    class Meta:
        model = GoodsCategory
        fields = ('id', 'title', 'parent', 'goodscategory_set', 'goods')

    def get_goods(self, obj):
        goods = Goods.objects.filter(category__parent=obj)
        serializer = RecommendedgoodsSerializer(goods, many=True)
        return serializer.data


# class GoodsListSerailizer(serializers.Serializer):
#     '''商品列表显示'''
#
#     cateroty_id = serializers.IntegerField(label='二级类别', min_value=1)
#     ordering = serializers.CharField(label='排序关键字', min_length=1)
#
#
#
class TwoCategorySerlializer(serializers.ModelSerializer):
    '''商品分类列表序列化器类'''
    goodscategory_set = CategorySerlializer(many=True)

    class Meta:
        model = GoodsCategory
        fields = ('id', 'title', 'parent', 'goodscategory_set')

